//时间插件渲染
layui.use('laydate', function () {
    var laydate = layui.laydate;
    laydate.render({
        elem: '#begintime'
        , type: 'datetime'
    });
    laydate.render({
        elem: '#endtime'
        , type: 'datetime'
    });
});


layui.use('form', function () {

    var condition_put_vo = {};

    var form = layui.form;

    //监听条件单选按钮
    form.on('radio(condition_radio)', function (data) {
        switch (data.value) {
            case '0':
                $("#condition_content").html('<div class="layui-form-item">' +
                    '            <label class="layui-form-label">参与最大次数</label>' +
                    '            <div class="layui-input-inline">' +
                    '                <input type="text" name="maxtry" required lay-verify="required|number" placeholder="请输入"' +
                    '                       class="layui-input">' +
                    '            </div>' +
                    '        </div>');


                break;
            case '1':
                $("#condition_content").html('<div class="layui-form-item">' +
                    '<label class="layui-form-label">积分数</label>' +
                    '<div class="layui-input-inline">' +
                    '<input type="text" name="points" required lay-verify="required|number"' +
                    '                           placeholder="积分数" autocomplete="off" class="layui-input">' +
                    '</div>' +
                    '</div>');
                break;
            case '2':
                $("#condition_content").html('<div class="layui-form-item">' +
                    '<label class="layui-form-label">选择数量</label>' +
                    '<div class="layui-input-inline">' +
                    '<select lay-verify="required" lay-filter="item_count_select">' +
                    '<option value="1">1</option>' +
                    '<option value="2">2</option>' +
                    '<option value="3">3</option>' +
                    '<option value="4">4</option>' +
                    '<option value="5">5</option>' +
                    '</select>' +
                    '</div>' +
                    '</div>' +
                    '<div id="condition_item" class="layui-form-item">' +
                    '<label class="layui-form-label">输入SKU</label>' +
                    '<div class="layui-input-inline">' +
                    '<input type="text" name="goods_key" required lay-verify="required"' +
                    '                       placeholder="" autocomplete="off" class="layui-input">' +
                    '</div>' +
                    '</div>' +

                    '<div class="layui-form-item">' +
                    '<label class="layui-form-label">开始时间</label> ' +
                    '<div class="layui-input-inline">' +
                    '<input class="layui-input" id="condition_begintime"> </div>' +
                    '</div>' +
                    '</div>' +
                    '<div class="layui-form-item">' +
                    '<label class="layui-form-label">结束时间</label> ' +
                    '<div class="layui-input-inline"> ' +
                    '<input class="layui-input" id="condition_endtime"> </div>' +
                    '</div>' +
                    '</div>' +
                    '</div>');
                layui.use('laydate', function () {
                    var laydate = layui.laydate;
                    laydate.render({
                        elem: '#condition_begintime'
                        , type: 'datetime'
                    });
                    laydate.render({
                        elem: '#condition_endtime'
                        , type: 'datetime'
                    });
                });
                form.render();
                break;
            default:
                return;
        }
    });

    //监听商品数量下拉列表
    form.on('select(item_count_select)', function (data) {
        var html_text = '<div class="layui-form-item">';
        for (var i = 0; i < data.value; i++) {
            html_text += '<label class="layui-form-label">输入SKU</label>' +
                '<div class="layui-input-inline">' +
                '<input type="text" name="goods_key" required lay-verify="required"' +
                '                       placeholder="输入SKU" autocomplete="off" class="layui-input">' +
                '</div>'
        }
        html_text += '</div>';
        $("#condition_item").html(html_text);
    });


    //条件表单提交
    form.on('submit(condition_form)', function (data) {

        var arr = $('[name="goods_key"]');

        var condition_good_list = [];

        arr.each(function (index, item) {
            condition_good_list.push(new goodsAndList($(this).val(),
                                    $('#condition_begintime').val(),
                                    $('#condition_endtime').val()));
        });

        data.field.condition_good_list = condition_good_list;

        condition_put_vo = data.field;

        layer.close(layer.index);

        $("#condition_open").addClass("layui-hide");
    });

    //主表单提交
    form.on('submit(table_form)', function (data) {
        data.field.condition_put_vo = condition_put_vo;

        $.ajax({
            url: 'http://' + IPADDRESS + ':' + PORT + '/table/putActivity',
            type: "POST",
            contentType: "application/json;charset=utf-8",
            data: JSON.stringify(data.field),
            dataType: "json",
            success: function (data) {
                if (data.header.res_code == '200000') {
                    layer.alert(data.header.message, {
                        icon: 1,
                        title: "提示"
                    });
                    setTimeout(function () {
                            // 获得frame索引
                            var index = parent.layer.getFrameIndex(window.name);
                            //关闭当前frame
                            parent.layer.close(index);
                        }, 800
                    );

                } else {
                    layer.alert(data.header.message, {
                        icon: 5,
                        title: "提示"
                    });
                }

            }
        });
    });
});

function add_condition() {
    $("#condition_open").removeClass("layui-hide");
    layer.open({
        type: 1,
        title: '添加条件',
        content: $('#condition_open'),
        area: ['400px', '300px'],
        success: function (layero, index) {

        },
        cancel: function (index, layero) {
            layer.close(index)
            $("#condition_open").addClass("layui-hide");
            return false;
        }
    });
}


function goodsAndList(goods_key, begintime, endtime) {
    this.goods_key = goods_key;
    this.begintime = begintime;
    this.endtime = endtime;
}
